package util

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

func OrmInit() error {
	debug, err := beego.AppConfig.Bool("debug")
	if err != nil {
		debug = false
	}
	if debug {
		orm.Debug = true
	}

	runmode :=beego.AppConfig.String("runmode")
	host := beego.AppConfig.String(runmode+"::db_host")
	port := beego.AppConfig.String(runmode+"::db_port")
	user := beego.AppConfig.String(runmode+"::db_user")
	password := beego.AppConfig.String(runmode+"::db_password")
	database := beego.AppConfig.String(runmode+"::db_database")
	dataSource := user+":"+password+"@tcp("+host+":"+port+")/"+database+"?charset=utf8"
	err = orm.RegisterDataBase("default", "mysql", dataSource, 30)
	return err
}